<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/28
 * Time: 17:15
 */

namespace app\admins\controller;

use app\admins\controller\BaseAdmin;

class Video extends BaseAdmin
{
    // 影片列表
    public function index(){
        $data['pageSize'] = 1;
        $data['page'] = max(1,(int)input('get.page'));

        $where = array();

        $data['wd'] = trim(input('get.wd'));
        $data['wd'] && $where='title like "%'.$data['wd'].'%"';
        $data['data'] = $this->db->table('video')->where($where)->pages($data['pageSize']);


        $label_ids = [];
        foreach ($data['data']['lists'] as $item) {
            !in_array($item['channel_id'],$label_ids) && $label_ids[] = $item['channel_id'];
            !in_array($item['charge_id'],$label_ids) && $label_ids[] = $item['charge_id'];
            !in_array($item['area_id'],$label_ids) && $label_ids[] = $item['area_id'];
        }
        $label_ids && $data['labels'] = $this->db->table('video_label')->where('id in('.implode(',',$label_ids).')')->cates('id');



        $this->assign('data',$data);
        return $this->fetch();
    }

    // 影片添加
    public function add(){
        $data['channel'] = $this->db->table('video_label')->where(array('flag'=>'channel','status'=>0))->lists();
        $data['charge'] = $this->db->table('video_label')->where(array('flag'=>'charge','status'=>0))->lists();
        $data['area'] = $this->db->table('video_label')->where(array('flag'=>'area','status'=>0))->lists();

        // 返回当前需编辑信息
        $id = (int)input('get.id');
        $data['item'] = $this->db->table('video')->where(array('id'=>$id))->item();

        $this->assign('data',$data);
        return $this->fetch();
    }
    // 图片上传
    public function upload_img(){
        $file = $this->request->file('file');
//        dump($file);
        if($file == null){
            exit(json_encode(array('code'=>1,'msg'=>'没有上传成功')));
        }

        $info = $file->move(ROOT_PATH.'public'.DS.'uploads');
        $ext = ($info->getExtension());
        if(!in_array($ext,array('jpg','jpeg','gif','png'))){
            exit(json_encode(array('code'=>1,'msg'=>'文件格式不支持')));
        }

        $img = '/uploads/'.$info->getSaveName();
        exit(json_encode(array('code'=>1,'msg'=>$img)));
    }

    public function delete(){
        $id = (int)input('post.id');
        $this->db->table('video')->where(array('id'=>$id))->delete();
        exit(json_encode(array('code'=>0,'msg'=>'删除成功')));
    }
    // 保存
    public function save(){

        $id = (int)input('post.id');

        $data['title'] = trim(input('post.title'));
        $data['channel_id'] = (int)input('post.channel_id');
        $data['charge_id'] = (int)input('post.charge_id');
        $data['area_id'] = (int)input('post.area_id');
        $data['url'] = trim(input('post.url'));
        $data['keywords'] = trim(input('post.keywords'));
        $data['desc'] = trim(input('post.desc'));
        $data['status'] = (int)input('post.status');
        $data['img'] = trim(input('post.img'));


        if($data['title'] == ''){
            exit(json_encode(array('code'=>1,'msg'=>"影片名称不能为空")));
        }

        if($data['url'] == ''){
            exit(json_encode(array('code'=>1,'msg'=>"影片地址不能为空")));
        }
        // 如果存在id则为修改  否者为新增
        if($id){
            $this->db->table('video')->where(array('id'=>$id))->update($data);
        }else{
            $data['add_time'] = time();
            $this->db->table('video')->insert($data);
        }

        exit(json_encode(array('code'=>0,'msg'=>"保存成功")));
    }
}